package views;
import util.Java_to_MySql;
import DAO.MangerDao;
import Entity.Manger;
import util.NotNull;
import util.Result;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class JuniorManagement {
    static  final  String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static  final  String DB_URL = "jdbc:mysql:// 172.17.3.199:3309/tour";
    static  final  String USER= "root";
    static  final  String PASS = "123";
    /*添加到数据库所必需的代码*/
    static Connection connect = null;
    static PreparedStatement cmmd = null;
    static ResultSet rs = null;
    static String sql = "";


    JLabel l4;
    JLabel l1;
    JLabel l2;
    JLabel l5;
    JLabel l6;
    JLabel l7;
    JLabel l8;
    JTextField ID;   //编号
    JTextField Name;  //姓名
    JTextField Account;   //账号
    JTextField Password;   //密码
    JTextField InterID;      //网点编号
    JTextField InterName;      //网点名称
    JTextField IsSenior;       //区分值
    JButton btnOK;           //保存
    JButton btn3;            //重置
    JButton breturn;
    TextArea area;
    int flag;
    Manger manger = new Manger();
    MangerDao mangerDao = new MangerDao();


    class PanelBg extends  JPanel{
        Dimension d;
        Image image;
        PanelBg(Dimension d,Image image){
            this.d=d;
            this.image=image;
        }
        @Override
        public void paintComponent(Graphics g){
            g.drawImage(image,0,0,d.width,d.height,this);
        }
    }
    Dimension fmsize=new Dimension(600,700);
    ImageIcon imageicon = new ImageIcon(this.getClass().getResource("/erhai4.jpg"));

    JuniorManagement(Result result) {
//        if ((result.getStatus().equals("1"))) {
//            ID.setText((String) result.getMessage());
//            String vID = ID.getText();
//        }
        JFrame jframe = new JFrame();
        jframe.setSize(600,700);
        jframe.setDefaultCloseOperation(jframe.DISPOSE_ON_CLOSE);
        jframe.setLayout(null);
        jframe.setLocationRelativeTo(null);


        JuniorManagement.PanelBg pContent = new JuniorManagement.PanelBg(fmsize,imageicon.getImage());
        pContent.setLayout(null);
        pContent.setBounds(0,0,600,700);
        jframe.getContentPane().add(pContent);



        l1 = new JLabel("编号");
        l1.setBounds(100,40,70,30);
        pContent.add(l1);

        ID = new JTextField();
        ID.setBounds(180,40,250,30);
        ID.setEnabled(false);
        pContent.add(ID);


        l2 = new JLabel("姓名");
        l2.setBounds(100,110,70,30);
        pContent.add(l2);

        Name = new JTextField();
        Name.setBounds(180,110,250,30);
        String vName = Name.getText();
        pContent.add(Name);


        l4 = new JLabel("账号");
        l4.setBounds(100,180,70,30);
        pContent.add(l4);

        Account = new JTextField();
        Account.setBounds(180,180,250,30);
        String vAccount = Account.getText();
        pContent.add(Account);


        l5 = new JLabel("密码");
        l5.setBounds(100,250,70,30);
        pContent.add(l5);

        Password = new JTextField();
        Password.setBounds(180,250,250,30);
        String vPassword = Password.getText();
        pContent.add(Password);


        l6 = new JLabel("网点编号");
        l6.setBounds(100,320,70,30);
        pContent.add(l6);

        InterID = new JTextField();
        InterID.setBounds(180,320,250,30);
        String vInterID  = InterID.getText();
        pContent.add(InterID);

        l7 = new JLabel("网点名称");
        l7.setBounds(100,390,70,30);
        pContent.add(l7);

        InterName = new JTextField();
        InterName.setBounds(180,390,250,30);
        String vInterName = InterName.getText();
        pContent.add(InterName);


        l8 = new JLabel("区分值");
        l8.setBounds(100,460,70,30);
        pContent.add(l8);

        IsSenior = new JTextField();
        IsSenior.setBounds(180,460,250,30);
//        String vvIsSenior = IsSenior.getText();
//        int vIsSenior = Integer.parseInt(vvIsSenior);
        pContent.add(IsSenior);

//        int flag=0;
        if ((result.getStatus().equals("1"))) {
            try {
                System.out.println(mangerDao.justgetID());
                ID.setText(Integer.valueOf(mangerDao.justgetID())+1+"");
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            flag=1;
        }
        if(result.getStatus().equals("0")){
            ID.setText((String) result.getMessage());
            String vID = ID.getText();
            manger=mangerDao.get_byID(vID);
            Name.setText(manger.getName());
            Account.setText(manger.getAccount());
            Password.setText(manger.getPassword());
            InterID.setText(manger.getInterID());
            InterName.setText(manger.getInterName());
            IsSenior.setText(String.valueOf(manger.isSenior()));
            flag=0;
        }

        btn3 =new JButton("保存");
        btn3.setBounds(180,570,70,50);
        pContent.add(btn3);
        btn3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if((new NotNull().IsNull(ID,Name,Password))==false){
                    JOptionPane.showMessageDialog(null,"不能为空");
                    return;
                }
                if (flag==1) {
//                    ID.setText((String) result.getMessage());
                    String vID = ID.getText();
                    String vName = Name.getText();
                    String vAccount = Account.getText();
                    String vPassword = Password.getText();
                    String vInterID  = InterID.getText();
                    String vInterName = InterName.getText();
                    String vvIsSenior = IsSenior.getText();
                    int vIsSenior = Integer.parseInt(vvIsSenior);

                    manger.setID(vID);
                    manger.setName(vName);
                    manger.setAccount(vAccount);
                    manger.setPassword(vPassword);
                    manger.setInterID(vInterID);
                    manger.setInterName(vInterName);
                    manger.setSenior(vIsSenior);
                    mangerDao.insert(manger);
                    System.out.println(manger.getAccount()+manger.getID());
                    JOptionPane.showMessageDialog(null,"新增成功");
                }
                if (flag==0){
//                    ID.setText((String) result.getMessage());
                    String vID = ID.getText();
                    String vName = Name.getText();
                    String vAccount = Account.getText();
                    String vPassword = Password.getText();
                    String vInterID  = InterID.getText();
                    String vInterName = InterName.getText();
                    String vvIsSenior = IsSenior.getText();
                    int vIsSenior = Integer.parseInt(vvIsSenior);
                    Manger manger = new Manger();
                    MangerDao mangerDao = new MangerDao();
                    manger.setID(vID);
                    manger.setName(vName);
                    manger.setAccount(vAccount);
                    manger.setPassword(vPassword);
                    manger.setInterID(vInterID);
                    manger.setInterName(vInterName);
                    manger.setSenior(vIsSenior) ;
                    mangerDao.update3(manger);
                    JOptionPane.showMessageDialog(null,"修改成功");
                }
            }
        });

        breturn = new JButton("退出");
        breturn.setBounds(270,570,70,50);
        breturn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                jframe.setVisible(false);
            }
        });
        pContent.add(breturn);


        btnOK = new JButton("重置");
        btnOK.setBounds(360,570,70,50);
        btnOK.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ID.setText("");
                Name.setText("");
                Account.setText("");
                Password.setText("");
                InterID.setText("");
                InterName.setText("");
                IsSenior.setText("");
            }
        });
        pContent.add(btnOK);

        jframe.setVisible(true);

    }

//    public static void main(String[] args) {
//        Result result = new Result();
//        result.setMessage("1");
//        result.setStatus("1");
//
//        new JuniorManagement(result);
//    }
}
